home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
4_0
/
DIALOG_W
/
DIALOG.C
Wrap
C/C++ Source or Header
|
1989-11-22
|
7KB
|
286 lines
/*
* dialog.c
*
* Copyright (c) 1989 Symantec Corporation. All rights reserved.
* by Darrell LeBlanc
*/
/* This is a demonstration of a scroll bar and a list in a dialog */
/* window, keeps track of the value, and demonstrates the value to */
/* the user. The DLOG resource it uses has three fields */
/* If you don't have the .rsrc file, it has a DLOG ID #130 and a DITL */
/* whose ID is 130. The DITL has four items: */
/* 1. an enabled OK Button. */
/* 2. a text edit field to display the current value */
/* 3. a user item 16 pixels wide, for the scroll bar to live in. */
/* 4. a user item used to display example of list in a dialog */
/* item numbers of things in DITL & DLOG */
#define OK_BUTTON 1
#define CURRENT_VALUE 2
#define SCROLL_BAR_ID 3
#define LIST_ID 4
/* globals */
ListHandle OnListHand = 0L;
Cell OldCell;
DialogPtr theDialog;
Boolean WaitABit;
ControlHandle myScroll, theControl;
int value;
int myControlValue;
Handle iHndle;
Rect dataBounds;
int NumCells = 20;
/* prototypes */
void OnLineClick(EventRecord myEvent);
pascal void myDraw (DialogPtr theDialog, int theItem);
pascal void ScrollProc (ControlHandle theControl, int thePart);
pascal Boolean myFilter(DialogPtr theDialog, EventRecord *theEvent, int *itemHit);
void DoOurDialog(void);
int main(void);
/* Called when a mousedown event occurs in the list of topics in the dialog */
void OnLineClick(EventRecord myEvent)
{
Cell lastCell;
OSErr myErr;
GlobalToLocal(&myEvent.where);
LClick(myEvent.where,myEvent.modifiers,OnListHand);
*(long *)&lastCell = LLastClick(OnListHand);
if(lastCell.v != OldCell.v)
OldCell.v = lastCell.v;
} /* OnLineClick */
pascal void myDraw (DialogPtr theDialog, int theItem)
{
DrawControls(theDialog);
}
/* Handles hits in the control that are not the thumb. Adjusts */
/* the display of the current display value. */
pascal void ScrollProc (ControlHandle theControl, int thePart)
{
int delta;
int oldValue;
int iType;
Rect iBox;
Handle iHndle;
int value;
Str255 myString;
Boolean DoChange;
long aLong;
if (WaitABit)
Delay(10, &aLong);
DoChange = true;
/* DebugStr("\pat switch"); */
switch (thePart)
{
case inUpButton:
delta = -1;
if ((GetCtlValue(myScroll)) == (GetCtlMin(myScroll)))
DoChange = false;
break;
case inDownButton:
delta = 1;
if ((GetCtlValue(myScroll)) == (GetCtlMax(myScroll)))
DoChange = false;
break;
case inPageUp:
if (WaitABit)
delta = -1;
else
delta = -5;
break;
case inPageDown:
if (WaitABit)
delta = 1;
else
delta = 5;
break;
} /* switch */
WaitABit = false;
if ((DoChange) && (thePart != 0))
{
oldValue = GetCtlValue(theControl);
SetCtlValue(theControl, oldValue + delta);
}
GetDItem(theDialog, CURRENT_VALUE, &iType, &iHndle, &iBox);
value = GetCtlValue(theControl);
if (myControlValue != value)
{
NumToString(value, myString);
SetIText(iHndle, myString);
myControlValue = value;
}
}
pascal Boolean myFilter(DialogPtr theDialog, EventRecord *theEvent, int *itemHit)
{
int iType;
Rect scroll_Box,list_Box;
Point mouseLoc;
Str255 myString;
int thePart;
Boolean DoTrack;
long along;
char cr;
WaitABit = true;
switch (theEvent->what)
{
case keyDown:
case autoKey: /* The default dialog dismissal.*/
{
cr = BitAnd(theEvent->message, charCodeMask);
if (( cr == 3) || (cr == 13)) /*<CR> or <ENTER>*/
{
GetDItem(theDialog, 1, &iType, &iHndle, &scroll_Box);
HiliteControl(iHndle, 1);
Delay(10, &along);
*itemHit = 1;
return true;
}
}
case mouseDown:
{
mouseLoc = theEvent->where;
GlobalToLocal(&mouseLoc);
GetDItem(theDialog, SCROLL_BAR_ID, &iType, &iHndle, &scroll_Box);/* scroll box*/
GetDItem(theDialog, LIST_ID, &iType,&iHndle, &list_Box); /* list box */
if (PtInRect(mouseLoc, &scroll_Box)) /*we know it is our scroll*/
{
/*Was mouse pressed in a control?*/
thePart = FindControl(mouseLoc, theDialog, &theControl);
if (theControl == myScroll)
{
if (thePart == inThumb)
{
/*Cannot use filter with thumb.*/
thePart = TrackControl(myScroll, mouseLoc, 0L);
/*Get and display the new value.*/
myControlValue = GetCtlValue(myScroll);
GetDItem(theDialog, CURRENT_VALUE, &iType, &iHndle, &scroll_Box);
NumToString(myControlValue, myString);
SetIText(iHndle, myString);
myControlValue = value;
}
else
/*Handled in ScrollProc.*/
thePart = TrackControl(myScroll, mouseLoc, ScrollProc);
} /* if (theControl == myScroll) */
} /* if PtInRect */
else if (PtInRect(mouseLoc, &list_Box)) /* mousedown evt in list */
OnLineClick(*theEvent);
} /* case mouseDown */
} /* switch */
} /* myFilter */
void DoOurDialog()
{
int iType;
Rect iBox;
Handle iHndle;
int itemNumber;
GrafPtr savePort;
Str255 myString;
int i;
Cell theCell;
Rect viewRect,cntrlRect,rView,box,dataBounds;
Point cSize;
int itemtype;
theDialog = GetNewDialog(130,0L,-1L);
GetPort(&savePort);
SetPort(theDialog);
/* frame the default OK button */
GetDItem(theDialog, OK_BUTTON, &iType, &iHndle, &iBox);
InsetRect(&iBox, -5, -5);
PenSize(3, 3);
FrameRoundRect(&iBox, 16, 16);
PenNormal();
/* set up the demo scroll bar */
GetDItem(theDialog, SCROLL_BAR_ID, &iType, &iHndle, &iBox);
SetDItem(theDialog, SCROLL_BAR_ID, iType, myDraw, &iBox);
myScroll = NewControl(theDialog, &iBox, "\pmyScroll", true, myControlValue, 0, 100, 16, 0);
/* set up static text box to show value of scroll bar */
GetDItem(theDialog, CURRENT_VALUE, &iType, &iHndle, &iBox);
NumToString(myControlValue, myString);
SetIText(iHndle, myString);
myControlValue = value;
/* set up list mgr example */
GetDItem(theDialog,LIST_ID,&itemtype,&iHndle,&box);
rView = box;
rView.left+=1; /* leave room for framerect */
rView.right-=16; /* room for scrool bar and framerect */
rView.bottom-=1; /* room for framerect */
cSize.h = rView.right - rView.left;
cSize.v = (rView.bottom - rView.top)/12; /* show 12 cells at a time */
/* to make it look good, you REALLY should set the height of each cell */
/* to be an even divisor of the total height of the user item box height */
/* but it is late, and I don't feel like figuring it out... */
SetRect(&dataBounds,0,0,1,NumCells);
OnListHand = LNew(&rView,&dataBounds,cSize,0,theDialog,true,false,false,true);
(**OnListHand).selFlags = lOnlyOne+lNoDisjoint+lNoExtend+lNoNilHilite;
for(i=0;i<NumCells;i++)
{
SetPt(&theCell,0,i);
LClrCell(theCell,OnListHand);
LSetCell("foo",3,theCell,OnListHand);
}
GetDItem(theDialog, LIST_ID, &itemtype, &iHndle, &box);
box.top-=1;
FrameRect(&box);
ModalDialog(myFilter, &itemNumber);
while(itemNumber != 1)
ModalDialog(myFilter,&itemNumber);
myControlValue = GetCtlValue(myScroll);
SetPort(savePort);
DisposeControl(myScroll);
DisposDialog(theDialog);
} /* DoOurDialog */
main()
{
InitGraf(&thePort);
InitFonts();
FlushEvents( everyEvent, 0 );
InitWindows();
InitMenus();
TEInit();
InitDialogs(0L);
InitCursor();
MaxApplZone();
myControlValue = 55;
DoOurDialog();
}